﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Windows.Threading;
using Budgetsch.ViewModels;

namespace Budgetsch.Views
{
    public partial class IncomesView : Page
    {
        private IncomesVM m_vm;
        public IncomesView()
        {
            InitializeComponent();
            m_vm = new IncomesVM();
            this.DataContext = m_vm;
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            var addForm = new IncomeDetailView(m_vm.GetNewAddIncomeVM());
            addForm.Show();
            if (addForm.DialogResult == true)
            {
                m_vm.RefreshIncomes();
            }
        }

        private void Remove_Click(object sender, RoutedEventArgs e)
        {
            m_vm.RemoveIncome(IncomesGrid.SelectedItem);
        }

        private void Edit_Click(object sender, RoutedEventArgs e)
        {
            var editForm = new IncomeDetailView(m_vm.GetNewIncomeEditorVM(IncomesGrid.SelectedItem));
            editForm.Show();
        }

        private void Excel_Click(object sender, RoutedEventArgs e)
        {

        }

        private void AddCategory_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                m_vm.AddCategory(CategoryTextBox.Text);
                CategoryTextBox.Text = string.Empty;
            }
        }

        private void YearCalendar_DisplayDateChanged(object sender, CalendarDateChangedEventArgs e)
        {
            if (e.AddedDate.HasValue)
            {
                m_vm.SelectedYear = e.AddedDate.Value.Year;
                YearCalendar.DisplayMode = CalendarMode.Decade;
            }
        }

        private void MonthCalendar_DisplayDateChanged(object sender, CalendarDateChangedEventArgs e)
        {
            if (e.AddedDate.HasValue)
            {
                m_vm.SelectedMonth = e.AddedDate.Value.Month;
                var dt = new DispatcherTimer()
                             {
                                 Interval = new TimeSpan(0, 0, 0, 0, 10)
                             };
                dt.Tick += new EventHandler(dt_Tick);
                dt.Start();
            }
        }

        //cheating because when year -> month "zoom" happens, the displaymode change only happens AFTER the displaydatechanged event risen
        void dt_Tick(object sender, EventArgs e)
        {
            ((DispatcherTimer) sender).Stop();
            MonthCalendar.DisplayMode = CalendarMode.Year;
        }
    }
}
